package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.order.client.OrderFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class SeckillController {
    @Autowired
    private ActivityFeignClient activityFeignClient;
    @Autowired
    private OrderFeignClient orderFeignClient;

    @GetMapping("seckill.html")
    public String seckill(Model model){
        Result result = activityFeignClient.findAll();
        model.addAttribute("list",result.getData());
        //秒杀首页
        return "seckill/index";
    }

    @GetMapping("seckill/{skuId}.html")
    public String seckillItem(@PathVariable Long skuId,Model model){
        Result result = this.activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item",result.getData());
        //商品详情页面
        return "seckill/item";
    }
    //抢购码
    @GetMapping("seckill/queue.html")
    public String seckillQueue(HttpServletRequest request){
        //获取skuId以及skuIdStr
        request.setAttribute("skuId",request.getParameter("skuId"));
        request.setAttribute("skuIdStr",request.getParameter("skuIdStr"));

        //返回排队页面
        return "seckill/queue";
    }
    //去下单
    @GetMapping("seckill/trade.html")
    public String seckillTrade(Model model){
        Result<Map> result = this.activityFeignClient.seckillTrade();
        if (result.isOk()){
            model.addAllAttributes(result.getData());
            return "seckill/trade";
        }else{
            model.addAttribute("message","订单结算页展示失败");
            return "seckill/fail";
        }
    }

}
